﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Domain.UserManageDomain;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoUser;

namespace Zocono.WCS.Application.AppUserManage
{
    public class UserApp
    {
        private readonly IUserService _Userservice;
        private readonly IUser _User;
        private readonly IMapper _Mapper;

        public UserApp(IUserService userService, IUser user, IMapper mapper)
        {
            _Userservice = userService;
            _User = user;
            _Mapper = mapper;
        }
        /// <summary>
        /// 只能返回基础类型、Dto
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public async Task<List<UserInfoDto>> GetUserListAsync(QueryUserDto where)
        {
            return await _Userservice.GetAllUserInfoAsync(where);
            //return _Mapper.Map<List<UserInfoDto>>(users);
        }

        public async Task<UserInfoDto> LoginAsync(string userName, string password)
        {
            var userinfoDto = await _User.LoginAsync(userName, password);
            return userinfoDto;
        }
        public async Task<UserInfoDto> LoginByIcAsync(string code)
        {
            var userinfoDto = await _User.LoginByIcAsync(code);
            return userinfoDto;
        }

        public async Task<bool> UpdatePasswordAsync(long ID, string newPassword, string oldPassword)
        {
            return await _User.UpdatePasswordAsync(ID, newPassword, oldPassword);
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="newPassword"></param>
        /// <param name="oldPassword"></param>
        /// <returns></returns>
        public async Task<bool> ResetPasswordAsync(long ID, string newPassword)
        {
            return await _User.ResetPasswordAsync(ID, newPassword);
        }


        public async Task<bool> InitPasswordAsync(List<string> userNames)
        {
            return await _User.InitPasswordAsync(userNames);
        }

        public async Task<long> AddUserInfoAsync(UserInfoDto userInfoDto)
        {
            return await _User.AddUserInfoAsync(userInfoDto);
        }

        public async Task<bool> UpdateUserInfoAsync(UserInfoDto userInfoDto)
        {
            return await _User.UpdateUserInfoAsync(userInfoDto);
        }

        public async Task<bool> DeleteUserInfoAsync(long ID)
        {
            return await _User.DeleteUserInfoAsync(ID);
        }

        public bool Add()
        {
            return _User.Add();
        }

        public async Task<UserInfoDto> GetUserInfoAsync(long Id)
        {
            return await _User.GetUserInfoAsync(Id);
        }

        #region 角色权限关系表
        public async Task<List<RoleFunctionInfoDto>> GetRoleFunctionInfoTreeAsync(long roleID)
        {
            return await _User.GetRoleFunctionInfoTreeAsync(roleID);
        }

        public async Task<List<FunctionInfoDto>> GetFunctionInfoAsync()
        {
            return await _Userservice.GetFunctionInfoAsync();
        }
        public async Task<List<FunctionInfoDto>> GetFunctionInfoAsync(FunctionInfoDto dto)
        {
            return await _Userservice.GetFunctionInfoAsync(dto);
        }
        /// <summary>
        /// 保存菜单列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> AddFunctionInfoAsync(FunctionInfoDto dto)
        {
            return await _Userservice.AddFunctionInfoAsync(dto);
        }
        /// <summary>
        /// 更新菜单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> UpdateFunctionInfoAsync(FunctionInfoDto dto)
        {
            return await _Userservice.UpdateFunctionInfoAsync(dto);
        }
        public async Task<FunctionInfoDto> GetFunctionInfoByIdAsync(long id)
        {
            return await _Userservice.GetFunctionInfoByIdAsync(id);
        }
        /// <summary>
        /// 根据上级菜单ID和状态查询菜单列表
        /// </summary>
        /// <param name="pId">上级菜单ID，0=顶级单</param>
        /// <param name="status">状态1=正常</param>
        /// <returns></returns>
        public async Task<List<FunctionInfoDto>> GetFunctionInfoListByPidAsync(long pId, int status)
        {
            return await _Userservice.GetFunctionInfoListByPidAsync(pId, status);
        }
        /// <summary>
        /// 修改菜单状态
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="status">状态1=启用，2=禁用</param>
        /// <returns></returns>
        public async Task<bool> UpdateStatusByIdAsync(long Id, int status)
        {
            return await _Userservice.UpdateStatusByIdAsync(Id, status);
        }
        public async Task<List<RoleFunctionInfoAllShow>> GetRoleFunctionInfoAllTreeAsync(long roleID)
        {
            return await _User.GetRoleFunctionInfoAllTreeAsync(roleID);
        }


        public async Task<List<RoleInfoDto>> GetRoleInfoAsync()
        {
            return await _Userservice.GetRoleInfoAsync();
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> addRoleAsync(RoleInfoDto dto)
        {
            return await _Userservice.addRoleAsync(dto);
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> UpdateRoleByIdAsync(RoleInfoDto dto)
        {
            return await _Userservice.UpdateRoleByIdAsync(dto);
        }
        /// <summary>
        /// 根据角色ID获取角色信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<RoleInfoDto> GetRoleInfoByIdAsync(long id)
        {
            return await _Userservice.GetRoleInfoByIdAsync(id);
        }
        /// <summary>
        /// 根据角色编号删除角色信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DelRoleByIdAsync(long id)
        {
            return await _Userservice.DelRoleByIdAsync(id);
        }

        public async Task<List<RoleFunctionInfoDto>> GetAllRoleFunctionInfoTreeAsync(long roleID)
        {
            return await _User.GetAllRoleFunctionInfoTreeAsync(roleID);
        }
        /// <summary>
        /// 根据角色ID获取菜单信息
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public async Task<List<RoleFunctionPage>> GetRoleFunctionByRoleId(long roleID)
        {
            return await _Userservice.GetRoleFunctionByRoleId(roleID);
        }
        /// <summary>
        /// 指保存角色权限信息
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task<bool> UpdateRoleFunctionInfoByRoleIdAsync(List<RoleFunctionInfoDto> list)
        {
            return await _Userservice.UpdateRoleFunctionInfoByRoleIdAsync(list);
        }
        #endregion


    }
}
